package com.lemon.reggie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lemon.reggie.common.BusinessException;
import com.lemon.reggie.pojo.dto.DefaultAddressBookDTO;
import com.lemon.reggie.pojo.dto.EditAddressBookDTO;
import com.lemon.reggie.pojo.dto.SaveAddressBookDTO;
import com.lemon.reggie.pojo.entity.AddressBook;
import com.lemon.reggie.mapper.AddressBookMapper;
import com.lemon.reggie.pojo.vo.AddressBookVO;
import com.lemon.reggie.service.AddressBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lemon.reggie.utils.BaseContextUtil;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 地址管理 服务实现类
 * </p>
 *
 * @author Lemon
 * @since 2022-05-23
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    /**
     * 添加地址
     *
     * @param saveAddressBookDTO
     * @return void
     * @author Guannankun
     * @since 2022/8/21
     */
    @Override
    public void saveAddressBook(SaveAddressBookDTO saveAddressBookDTO) {
        AddressBook addressBook = new AddressBook();
        BeanUtil.copyProperties(saveAddressBookDTO, addressBook);
        addressBook.setUserId(BaseContextUtil.getCurrentId());
        this.save(addressBook);
    }

    /**
     * 登录用户地址集合
     *
     * @return java.util.List<com.lemon.reggie.pojo.vo.AddressBookVO>
     * @author Guannankun
     * @since 2022/8/21
     */
    @Override
    public List<AddressBookVO> listAddressBook() {
        //查询当前用户不是默认地址的集合
        List<AddressBook> list = this.list(new LambdaQueryWrapper<AddressBook>()
                .eq(AddressBook::getUserId, BaseContextUtil.getCurrentId())
                .eq(AddressBook::getIsDefault, 0)
                .orderByDesc(AddressBook::getUpdateTime));

        if (CollUtil.isNotEmpty(list)) {
            List<AddressBookVO> addressBookVOS = list.stream().map(addressBook -> {
                AddressBookVO addressBookVO = new AddressBookVO();
                BeanUtil.copyProperties(addressBook, addressBookVO);
                return addressBookVO;
            }).collect(Collectors.toList());

            AddressBookVO defaultAddress = this.getDefaultAddress();
            if (Objects.nonNull(defaultAddress)) {
                //默认地址不为空，就放在首位
                addressBookVOS.add(0, defaultAddress);
            }
            return addressBookVOS;
        }
        return Collections.emptyList();
    }

    /**
     * 根据id获取地址
     *
     * @param id
     * @return com.lemon.reggie.pojo.vo.AddressBookVO
     * @author Guannankun
     * @since 2022/8/21
     */
    @Override
    public AddressBookVO getAddressBookVO(Long id) {
        AddressBook addressBook = this.getById(id);
        AddressBookVO addressBookVO = new AddressBookVO();
        BeanUtil.copyProperties(addressBook, addressBookVO);
        return addressBookVO;
    }

    /**
     * 设置默认地址
     *
     * @param defaultAddressBookDTO
     * @return void
     * @author Guannankun
     * @since 2022/8/21
     */
    @Override
    public AddressBookVO defaultAddressBook(DefaultAddressBookDTO defaultAddressBookDTO) {
        AddressBook addressBook = this.getById(defaultAddressBookDTO.getId());
        if (Objects.isNull(addressBook)) {
            throw new BusinessException("当前设置的地址不存在！");
        }

        //1. 查询当前用户原来设置的默认地址
        AddressBook one = this.getOne(new LambdaQueryWrapper<AddressBook>().eq(AddressBook::getUserId,
                BaseContextUtil.getCurrentId()).eq(AddressBook::getIsDefault, 1));

        if (Objects.nonNull(one)) {
            //当前用户设置了默认地址
            //移除原来设置的默认地址，并设置新的默认地址
            one.setIsDefault(0);
            this.updateById(one);
        }

        //设置当前传递的为默认地址
        addressBook.setIsDefault(1);
        this.updateById(addressBook);

        /*if (Objects.isNull(one)) {

            addressBook.setIsDefault(1);
            this.updateById(addressBook);
        } else {
            //当前用户设置了默认地址
            //移除原来设置的默认地址，并设置新的默认地址
            one.setIsDefault(0);
            this.updateById(one);
            addressBook.setIsDefault(1);
            this.updateById(addressBook);
        }*/

        AddressBookVO addressBookVO = new AddressBookVO();
        BeanUtil.copyProperties(addressBook, addressBookVO);
        return addressBookVO;
    }

    /**
     * 修改地址
     *
     * @param editAddressBookDTO
     * @return void
     * @author Guannankun
     * @since 2022/8/21
     */
    @Override
    public void editAddressBook(EditAddressBookDTO editAddressBookDTO) {
        AddressBook addressBook = this.getById(editAddressBookDTO.getId());
        if (Objects.isNull(addressBook)) {
            throw new BusinessException("修改的地址不存在！");
        }

        BeanUtil.copyProperties(editAddressBookDTO, addressBook);
        this.updateById(addressBook);
    }

    /**
     * 删除地址
     *
     * @param ids
     * @return void
     * @author Guannankun
     * @since 2022/8/21
     */
    @Override
    public void deleteAddressBook(List<Long> ids) {
        if (CollUtil.isNotEmpty(ids)) {
            this.removeByIds(ids);
        }
    }

    /**
     * 获取当前用户的默认地址
     * @return com.lemon.reggie.pojo.vo.AddressBookVO
     * @author Guannankun
     * @since 2022/8/29
     */
    @Override
    public AddressBookVO getUserDefault() {
        AddressBook addressBook = this.getOne(new LambdaQueryWrapper<AddressBook>().eq(AddressBook::getUserId, BaseContextUtil.getCurrentId()).eq(AddressBook::getIsDefault, 1));
        if (Objects.isNull(addressBook)) {
            throw new BusinessException("当前用户没有设置默认地址！");
        }
        AddressBookVO addressBookVO = new AddressBookVO();
        BeanUtil.copyProperties(addressBook, addressBookVO);
        return addressBookVO;
    }

    /**
     * 获取当前用户的默认地址
     * @return com.lemon.reggie.pojo.vo.AddressBookVO
     * @author Guannankun
     * @since 2022/8/21
     */
    private AddressBookVO getDefaultAddress() {
        AddressBook one = this.getOne(new LambdaQueryWrapper<AddressBook>()
                .eq(AddressBook::getUserId, BaseContextUtil.getCurrentId())
                .eq(AddressBook::getIsDefault, 1));
        if (Objects.nonNull(one)) {
            return AddressBookVO.builder()
                    .id(one.getId())
                    .consignee(one.getConsignee())
                    .phone(one.getPhone())
                    .isDefault(one.getIsDefault())
                    .detail(one.getDetail())
                    .sex(one.getSex())
                    .label(one.getLabel())
                    .userId(one.getUserId())
                    .build();
        }
        return null;
    }
}
